#region System

using System;
using System.Diagnostics;
using System.IO;

using CUtil;


#endregion

namespace CUtil
{
	/// <summary>
	/// Summary description for FileUtil.
	/// </summary>
	public class IOUtil
	{
		#region Constructors and Destructors 

		public IOUtil()
		{
		}

		#endregion

        public static void SplitSymbolAndRegion(string strName, out string symbol, out string region)
        {
             
            string[] arrStr = strName.Split(new Char[] { '.' }, 100);

            symbol = arrStr[0].ToString();
            region = arrStr[1].ToString(); 
            

        }
        //public static string SplitSymbolFromRegion(string strName)
        //{
        //    string[] arrStr = strName.Split(new Char[] { '.' }, 100);

        //    return arrStr[0].ToString();


        //}
        public static string GetRelativeDirectory(string pathFull)
        {
            string[] arrStr = pathFull.Split(new Char[] { '\\' }, 100);
            string relativeDir = arrStr[arrStr.GetLength(0) - 1];
            return relativeDir;
        }
        public static string GetNameBaseFromNameWithInterval(string strName)
        {
            string[] arrStr = strName.Split(new Char[] { '_' }, 100);
            string str = arrStr[0].ToString();
            for (int i = 1; i < (arrStr.GetLength(0) -1); i++)
            {
                str += "_" + arrStr[i].ToString() ;
            }
            
            return str;


        }
        public static string ExtractNameFromShortFileName(string strName)
        {
            string[] arrStr = strName.Split(new Char[] { '.' }, 100);
            string str = arrStr[0];
            return str;
        }
        public static string GetNameBaseFromFileName(string strName)
        {
            string str = ExtractNameFromShortFileName(strName);
            str = GetNameBaseFromNameWithInterval(str);

            return str;
        }
        public static int GetIntervalFromNameWithInterval(string strName)
        {
            string[] arrStr = strName.Split(new Char[] { '_' }, 100);
            if (arrStr == null && arrStr.GetLength(0) < 1)
                return 0;
            try
            {
                int MyPeriod = Convert.ToInt32(arrStr[arrStr.GetLength(0) - 1].ToString());
                return MyPeriod;
            }
            catch
            {
                // no problem - there is no interval in the name configured - this is for instance
                //a CompanyReport indicator of 
                return 2;
            }
        }

		public static string ExtractNameFromFile(System.IO.FileInfo file)
		{
			string[] arrSplit = file.Name.Split(new char[]{'.'} , 100);
			string fileName = arrSplit[0] + "." + arrSplit[1] + "." + arrSplit[2];
			return fileName;


		}
		public static string ExtractNameFromFileName(string fileNameLong)
		{
            string[] arrSplit = fileNameLong.Split(new char[] { '.' }, 100);
			string name = arrSplit[0] ;
			return name ;


		}
        public static string ExtractNameFromFileName_New(string fileName)
        {
            string[] arrSplit = fileName.Split(new char[] { '.' }, 100);
            string name = arrSplit[0];
            return name;


        }
        public static string ExtractIDFromFileNameLong(string fileNameLong)
        {
            string[] arrSplit = fileNameLong.Split(new Char[] { '\\' }, 100);
            string fileNameShort = arrSplit[arrSplit.GetLength(0) - 1];

            string[] arrSplit1 = fileNameShort.Split(new char[] { '.' }, 100);
            string name = arrSplit1[0];
            return name;


        }
        public static string CutRightFourChars(string fileName)
        {
            //string[] arrSplit = fileName.Split(new char[] { '.' }, 100);
            string name = fileName.Remove(fileName.Length - 4, 4);
            //string name = fileName.Remove(fileName.Length - 5, 4);
            return name;
            

        }
        public static string ExtractNameWithoutExtensionFromFileName(string fileName)
        {
            string[] arrStr = fileName.Split(new Char[] { '\\' }, 100);
            string strFileNameShort = arrStr[arrStr.GetLength(0) - 1];

            string[] arrSplit = strFileNameShort.Split(new char[] { '.' }, 100);
            string name = arrSplit[0];
            return name;


        }
        public static string ExtractDirectoryFromFileName(string fileName)
        {
            string[] arrSplit = fileName.Split(new char[] { '.' }, 100);
            System.Text.StringBuilder str = new System.Text.StringBuilder();
            for (int i = 0; i < arrSplit.GetLength(0) - 2; i++)
            {
                str.Append(arrSplit[i]);
            }

            return str.ToString();



        }
        public static string ExtractFullPathFromFileName(string fileNameIn)
        {
            string[] arrSplit = fileNameIn.Split(new Char[] { '\\' }, 100);

            string dirName = string.Empty;
            System.Text.StringBuilder str = new System.Text.StringBuilder();
            for (int i = 0; i < arrSplit.GetLength(0) - 1; i++)
            {

                str.Append(arrSplit[i] + @"\");
               
            }
            str = str.Remove(str.Length - 1, 1);
            dirName = str.ToString();

            return dirName;

        }
        public static void ExtractDirectoryAndNameFromFileName(string fileNameIn, ref string fileNameShort, ref string dirName)
        {
            string[] arrSplit = fileNameIn.Split(new Char[] { '\\' }, 100);
           fileNameShort = arrSplit[arrSplit.GetLength(0) - 1];


            System.Text.StringBuilder str = new System.Text.StringBuilder();
            for (int i = 0; i < arrSplit.GetLength(0) - 2; i++)
            {

                str.Append(arrSplit[i] + @"\");
                //str.AddString(.StringToAddAddString(arrSplit[i]);



            }

            dirName = str.ToString();



        }
        public static string CutLastTwoDirectories(string dirIn)
        {
            string[] arrSplit = dirIn.Split(new Char[] { '\\' }, 100);
            
            System.Text.StringBuilder str = new System.Text.StringBuilder();
            for (int i = 0; i < arrSplit.GetLength(0) - 2; i++)
            {

                str.Append(arrSplit[i] + @"\");
                //str.AddString(.StringToAddAddString(arrSplit[i]);



            }

            return str.ToString();



        }

        public static string ExtractBetween_(string fileName, int which)
        {
            string[] arrSplit = fileName.Split(new char[] { '_' }, 100);
            string name = string.Empty;
            if (arrSplit.GetLength(0) >= which)
                name = arrSplit[which];
            return name;


        }
	}
	#region Class FileNameCorrector

	/// <summary>
	/// Summary description for CFileNameCorrector.
	/// </summary>
	public class FileNameCorrector
	{
			#region Constructors and Destructors

		/// <summary>
		/// FileNameCorrector
		/// </summary>
		public FileNameCorrector()
		{
		}

			#endregion

			#region Methods Public

		/// <summary>
		/// GetCorrectFileName
		/// Removes "/" and "\" from a FileName
		/// </summary>
		/// <param name="parFileName"></param>
		/// <returns></returns>
	

			#endregion
	}

		#endregion

	#region Class Directory

	/// <summary>
	/// Summary description for CDirectory.
	/// </summary>
	public class DirectoryUtil
	{
			#region Constants

		public const string DirectoryDelimiter = @"\";

			#endregion

			#region Constructors and Destructors

		/// <summary>
		/// Directory
		/// </summary>
		public DirectoryUtil()
		{
		}

		/// <summary>
		/// CreateDirectory
		/// </summary>
		/// <param name="parDirectoryPath"></param>
		public static void CreateDirectory(string parDirectoryPath)
		{
			
			if (! System.IO.Directory.Exists(parDirectoryPath))
			{
				System.IO.Directory.CreateDirectory(parDirectoryPath);
			}
		}
			#endregion

			#region Methods Public

	


		public static System.Collections.ArrayList GetXMLFiles(string myPath)
		{
			System.Collections.ArrayList files = new System.Collections.ArrayList();
			DirectoryInfo directoryInfo = new DirectoryInfo(myPath);
			try
			{
				FileInfo[] farr = directoryInfo.GetFiles("*.xml");
				if(farr != null && farr.GetLength(0) > 0)
				{
					for(int i = 0; i < farr.GetLength(0); i++)
					{
						FileInfo fileInfo = farr[i];
						//string portfolioFileName= portfolioFullFileName.Substring(0,portfolioFullFileName.IndexOf('.'));
						files.Add(fileInfo.Name);

					}
				}
			}
			catch
			{
				//no files there
				//System.Diagnostics.Debug.Assert(false,"error reading files");
				System.Diagnostics.Debug.WriteLine("No Files in directory " + myPath);

			}
			return files;
		}

			#endregion
	}

		#endregion

		#region Class StreamWriter

	/// <summary>
	/// Summary description for StreamWriter.
	/// </summary>
//	public class StreamWriterUtil : Object
//	{
//			#region Fields
//
//		/// <summary>
//		/// streamName
//		/// </summary>
//		private string streamName;
//		/// <summary>
//		/// systemStreamWriter
//		/// </summary>
//		private System.IO.StreamWriter systemStreamWriter;
//
//			#endregion
//
//			#region Constructors and Destructors
//
//		/// <summary>
//		/// StreamWriter
//		/// </summary>
//		/// <param name="parFullFilePath"></param>
//		/// <param name="parFileName"></param>
//		public StreamWriterUtil(string parFullFilePath,string parFileName): this(parFullFilePath,parFileName,false)
//		{
//		}
//		/// <summary>
//		/// StreamWriter
//		/// </summary>
//		/// <param name="parFullFilePath"></param>
//		/// <param name="parFileName"></param>
//		/// <param name="parCreatePath"></param>
//		public StreamWriterUtil(string parFullFilePath,string parFileName,bool parCreatePath) : this(parFullFilePath,parFileName,parCreatePath,false)
//		{
//
//		}
//		/// <summary>
//		/// StreamWriter
//		/// </summary>
//		/// <param name="parFullFilePath"></param>
//		/// <param name="parFileName"></param>
//		/// <param name="parCreatePath"></param>
//		/// <param name="parAppend"></param>
//		public StreamWriterUtil(string parFullFilePath,string parFileName,bool parCreatePath,bool parAppend)
//		{
//			this.InitStreamWriter(parFullFilePath,parFileName,parCreatePath,parAppend);
//		}
//		/// <summary>
//		/// InitStreamWriter
//		/// </summary>
//		/// <param name="parFullFilePath"></param>
//		/// <param name="parFileName"></param>
//		/// <param name="parCreatePath"></param>
//		private void InitStreamWriter(string parFullFilePath,string parFileName , bool parCreatePath,bool parAppend)
//		{
//			try
//			{
//				if (parCreatePath)
//				{
//					DirectoryUtil.CreateDirectory(parFullFilePath);
//				}
//				string locFullFileName = parFullFilePath + CUtil.DirectoryUtil.DirectoryDelimiter + parFileName;
//				this.streamName = locFullFileName;
//				this.systemStreamWriter = new System.IO.StreamWriter(locFullFileName,parAppend);
//				//Debug.WriteLine("Opening Stream " + this.streamName);
//			}
//			catch (Exception exc)
//			{
//				Debug.WriteLine("Error creating StreamWriter: " +exc.Message);
//			}
//		}
//
//			#endregion
//
//			#region Methods Private
//
//	
//
//			#endregion
//
//			#region Methods Public
//
//		/// <summary>
//		/// Close
//		/// </summary>
//		public void Close()
//		{
//			try
//			{
//				if (this.systemStreamWriter != null)
//				{
//					this.systemStreamWriter.Close();
//				}
//				//Debug.WriteLine("Closing Stream " + this.streamName);
//			}
//			catch (Exception exc)
//			{
//				Debug.WriteLine("Error closing StreamWriter: " + exc.Message);
//			}
//		}
//		/// <summary>
//		/// Write
//		/// </summary>
//		/// <param name="parLineString"></param>
//		public void Write(string parLineString)
//		{
//			try
//			{
//				this.systemStreamWriter.Write(parLineString);
//			}
//			catch (Exception exc)
//			{
//				Debug.WriteLine("Error writing to Stream: " + exc.Message);
//			}
//		}
//		/// <summary>
//		/// WriteLine
//		/// </summary>
//		/// <param name="parLineString"></param>
//		public void WriteLine(string parLineString)
//		{
//			try
//			{
//				this.systemStreamWriter.WriteLine(parLineString);
//			}
//			catch (Exception exc)
//			{
//				Debug.WriteLine("Error writing " + parLineString + " to Stream: " + "\n" + exc.Message);
//			}
//		}
//
//			#endregion
//	}

		#endregion
}
